import 'package:get/get.dart';
import 'package:honeypotflutter/modules/autonym/api/autonymApi.dart';
import 'package:honeypotflutter/public/public_alert.dart';
import 'package:honeypotflutter/public/public_get.dart';
import 'package:honeypotflutter/public/public_image.dart';
import 'package:honeypotflutter/publicBussine/loginInfo/bussine_userInfo.dart';
import 'package:honeypotflutter/publicBussine/upload/bussine_oss.dart';
import 'package:image_picker/image_picker.dart';
import '../../help/autonym_help.dart';
import '../../model/autonym_image_info.dart';
import '../autonym_express/view.dart';
import '../autonym_success/view.dart';

class AutonymImageLogic extends GetxController {
  /// 前图片
  AutonymImageInfo? frontInfo;

  /// 后图片
  AutonymImageInfo? reverseInfo;

  /// 人像图片
  AutonymImageInfo? personInfo;

  /// 点击按钮
  RxBool enable = RxBool(false);

  /*
  * 选择图片
  * */
  selectImage(AutonymSelectImageType type) async {
    HLAlert.showSheet(
      title: "请选择类型",
      menuList: ["从相册获取", "从相机获取"],
      action: (int index, String text) async {
        XFile file;
        if (index == 0) {
          List<XFile> result = await HLCameraImage.photo();
          file = result.first;
        } else {
          file = await HLCameraImage.camera();
        }

        String host = await MGBussineOSS.uploadImage(file);

        switch (type) {
          case AutonymSelectImageType.front:
            frontInfo = AutonymImageInfo(file: file, host: host);
            break;

          case AutonymSelectImageType.reverse:
            reverseInfo = AutonymImageInfo(file: file, host: host);
            break;

          case AutonymSelectImageType.person:
            personInfo = AutonymImageInfo(file: file, host: host);
            break;
        }

        /// 是否可以点击
        enable.value = (frontInfo != null) && (reverseInfo != null) && (personInfo != null);
        update();
      },
    );
  }

  /*
  * 删除图片
  * */
  deleteImage(AutonymSelectImageType type) async {
    switch (type) {
      case AutonymSelectImageType.front:
        frontInfo = null;
        break;

      case AutonymSelectImageType.reverse:
        reverseInfo = null;
        break;

      case AutonymSelectImageType.person:
        personInfo = null;
        break;
    }

    /// 是否可以点击
    enable.value = (frontInfo != null) && (reverseInfo != null) && (personInfo != null);
    update();
  }

  /*
  * 提交实名认证
  * */
  submitAction() async {
    Map<String, String> realNameInfo = Get.arguments;
    Map<String, dynamic> param = {
      "realName": realNameInfo["realName"],
      "idCardNumber": realNameInfo["idCardNumber"],
      "idCardFrontView": frontInfo?.host,
      "idCardRearView": reverseInfo?.host,
      "idHandheldView": personInfo?.host,
    };
    await AutonymApi.submitRealName(param);
    UserInfoLogic _userLogic = Get.find<UserInfoLogic>();
    if (_userLogic.userInfo.courierState == 6 || _userLogic.userInfo.courierState == 11) {
      HLGet.to(AutonymSuccessPage());
      return;
    }
    HLGet.to(AutonymExpressPage());
  }
}
